﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace bakupDataBase
{
    class Program
    {
        static void Main(string[] args)
        {
            string dataDir = @"I:\database\newbak\"; //备份文件保存目录
            
            string fileName = bakupData("newMyadData", dataDir);
            compressData(fileName);

            fileName = bakupData("myadWorkDatabase", dataDir);
            compressData(fileName);

            //Console.ReadKey();
        }

        /// <summary>
        /// 压缩文件到同目录
        /// </summary>
        private static void compressData(string fileName)
        {
            string path = @"C:\Program Files\WinRAR\WinRAR.exe"; //winrar文件绝对路径
            string para = @"a  -df -m1 " + fileName + ".rar" + " " + fileName; //df 压缩后删除原文件；m1最快压缩方式
            LIB.StartProcess.startProcess(path, para);
            Console.WriteLine("compress ok");
        }

        /// <summary>
        /// 把数据库备份到指定文件
        /// </summary>
        private static string bakupData(string dataBase, string dataDir)
        {
            string dwdt = DateTime.Today.ToString("yyyyMMdd");
            string fileName = dataDir + dataBase + dwdt + ".bak";
            string sql_bakup = "BACKUP DATABASE "+dataBase+" TO DISK = '" + fileName + "'";
            
            LIB.DataAcess worker = new LIB.DataAcess();
            Console.WriteLine("bak up begin.");
            worker.Execute(sql_bakup);
            Console.WriteLine(fileName + "bak up ok");
            return fileName;
        }
    }
}
